<?php

namespace app\home\controller;

use app\common\controller\Oauth;
use external\SFTools;
use think\facade\Log;

class Register extends Oauth {

    public function initialize() {
        parent::initialize();
    }

    public function sub() {
        if (!request()->isPost()) {
            $this->error(SFTools::err_msg('3301'));
            exit;
        }
        $data = input('post.');
        $admin_account = htmlspecialchars($data['login_account']);
        $admin_pwd = htmlspecialchars($data['login_pass']);
        $adminLogic = new \backend\logic\AdminLogic;
        $adminFind = $adminLogic->getAdminFindBuyAdminUUID($admin_account);
        if (empty($adminFind)) {
            $this->error(SFTools::err_msg('1004'));
            exit;
        }
        if (md5($admin_pwd) !== $adminFind['admin_password']) {
            $this->error(SFTools::err_msg('1005'));
            exit;
        }
        if ($adminFind['admin_state'] != 1) {
            $this->error(SFTools::err_msg('1006'));
            exit;
        }
        $userFindNew = array();
        $user_uuid = $adminFind['admin_uuid'];
        $userFindNew['user_uuid'] = $user_uuid;
        $cookie_key = SFTools::getCookieKey();
        $cache_key_str = SERVICE_APP_ID . $user_uuid;
        $cache_key = md5($cache_key_str);
        setcookie($cookie_key, $cache_key, 0, '/');
        $auth = false;
        $level = NULL;
        $data_auth = NULL;
        $f = 'auth';
        for ($i = 1; $i <= 10; $i++) {
            if ($i > 9) {
                $key = $f . $i;
            } else {
                $key = $f . '0' . $i;
            }
            if (empty($adminFind[$key])) {
                break;
            }
            if($adminFind[$key] === 'ONE'){
                continue;
            }
            $auth = $key;
            $level = $i;
            $auth_str = $adminFind[$key];
            if(strpos($auth_str, 'PART#') !== false){
                $data_auth = substr($auth_str, 5);
                break;
            }
            if($auth_str === 'ALL'){
                $data_auth = 'ALL';
                break;
            }
        }
        Log::record("sub setTicket adminFind: " . json_encode($adminFind), 'alert');
        if ($auth === false) {
            $this->error(SFTools::err_msg('1003'));
            exit;
        }
        $url = '/system/' . ucfirst($auth) . '/index.html';
        $userFindNew['user_auth'] = $auth;
        $userFindNew['account'] = $admin_account;
        $userFindNew['user_level'] = $level;
        $userFindNew['data_auth'] = $data_auth;
        Log::record("sub setTicket userFindNew: " . json_encode($userFindNew), 'alert');
        $this->setTicket($userFindNew);
        $this->redirect($url);
        /*$url='/home/Login/index.html';
        $adminLogic = new \backend\logic\AdminLogic;
        $adminFind = $adminLogic->loginCheck($data);
        if(!empty($adminFind)){
            $url='/brand/Forward/index.html';
        }
        $this->redirect($url);*/
    }

    public function logout() {
        $this->user_logout();
    }
}
